package DP;

public class IsSubsequence {
    public static void main(String[] args) {
        System.out.println(isSubsequence1("acb", "ahbgdc"));
    }

    public static boolean isSubsequence(String s, String t) {
        if (s.length() == 0) {
            return true;
        }

        int[][] dp = new int[s.length() + 1][t.length() + 1];
        for (int i = 1; i < s.length() + 1; i++) {
            for (int j = 1; j < t.length() + 1; j++) {
                if (s.charAt(i - 1) == t.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }

        return dp[s.length()][t.length()] == s.length();
    }

    public static boolean isSubsequence1(String s, String t) {
        if (s.length() == 0) {
            return true;
        }else if(t.length()==0){
            return false;
        }
        int j = 0;
        for (int i = 0; i < s.length(); i++, j++) {
            if(j>=t.length()){
                return false;
            }
            while (s.charAt(i) != t.charAt(j)) {
                j++;
                if (j >= t.length()) {
                    return false;
                }
            }

        }
        return true;
    }
}
